package com.zhou.config;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpConfig {

    /**
     * 配置http连接池信息
     */
    @Bean
    public PoolingHttpClientConnectionManager httpClientConnectionManager() {
        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
        manager.setMaxTotal(200);
        return manager;
    }

    /**
     * 配置 clientBuilder，如果要创建一个httpClient对象，可以使用这个类进行创建
     * 使用builder.build方法。
     */
    @Bean
    public HttpClientBuilder httpClientBuilder(PoolingHttpClientConnectionManager clientManager) {
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(3000) //建立连接的超时时间
                .setSocketTimeout(1000) //长时间未响应的超时时间
                .setConnectionRequestTimeout(3000) //设置从连接池获取连接的超时时间
                .build();
        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setConnectionManager(clientManager);
        builder.setDefaultRequestConfig(requestConfig);
        return builder;
    }


}
